package com.afdor.rws.event.driven.channel.aliyun.mns.utils;

import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;

/**
 * 阿里云MNS消息服务相关工具类
 *
 * @author 悭梵
 * @date Created in 2018-12-27 16:51
 */
public final class AliyunMnsUtils {
    public static final String DEFAULT_COMPONENT_CONFIG_EXTENSION_NAME = "aliyunMns";


    /**
     * 主题名称
     *
     * @param eventSourceName
     * @return
     */
    public static String getTopicName(@NonNull String eventSourceName) {
        // AMQ 替换"_"
        return "topic" + getCamelCase(true, StringUtils.split(StringUtils.replace(eventSourceName, "_", "."), "."));
    }

    /**
     * 主题子队列名称
     *
     * @return
     */
    public static String getTopicSubQueueName(@NonNull String eventSourceName, @NonNull String consumerName) {
        // AMQ 新建队列名称替换"_"
        return "subQueue" + getCamelCase(true, StringUtils.split(StringUtils.replace(StringUtils.replace(eventSourceName, "_", "."), ".", "$"), "$")) + "-" + getCamelCase(true, StringUtils.split(StringUtils.replace(consumerName, "_", "."), "."));
    }


    /**
     * 队列名称
     *
     * @return
     */
    public static String getQueueName(@NonNull String eventSourceName) {
        // AMQ 新建队列名称替换"_"
        return "queue" + getCamelCase(true, StringUtils.split(StringUtils.replace(StringUtils.replace(eventSourceName, "_", "."), ".", "$"), "$"));
    }


    /**
     * 返回一个驼峰变量
     *
     * @return
     */
    public static String getCamelCase(boolean largeCamelCase, String... args) {
        StringBuilder builder = new StringBuilder(100);
        for (String s : args) {
            if (StringUtils.isNotBlank(s)) {
                s = s.trim();
                if (s.length() == 1) {
                    builder.append(Character.toUpperCase(s.charAt(0)));
                } else {
                    builder.append(Character.toUpperCase(s.charAt(0)));
                    builder.append(StringUtils.substring(s, 1).toLowerCase());
                }
            }
        }
        if (!largeCamelCase && builder.length() > 0) {
            return Character.toUpperCase(builder.charAt(0)) + builder.substring(1);
        }
        return builder.toString();
    }
}
